﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace GFluentCode.Tools
{
    /// <summary>
    /// 系统语音
    /// </summary>
    public class SpeakHelper
    {
        /// <summary>
        /// 用系统语音播读内容
        /// </summary>
        /// <param name="text"></param>
        public static void Speak(string text)
        {
            var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "audio.vbs");

            var str = $"CreateObject(\"SAPI.SpVoice\").Speak\"{text}\"";

            using (var fs = new FileStream(path, FileMode.OpenOrCreate))
            {
                fs.SetLength(0);
                using (var sw = new StreamWriter(fs, Encoding.Default))
                {
                    sw.Write(str);
                    sw.Close();
                }
                fs.Close();
            }

            try
            {
                var p = System.Diagnostics.Process.Start(path);
                p.Close();

                File.Delete(path);
            }
            catch { }
        }
    }
}
